package com.scsystem.xysc.controller;

import com.scsystem.xysc.service.SendssY;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@Transactional
@CrossOrigin
@Tag(name = "发送短信")
@RequestMapping("/sendss")
public class SendssController {
    @Autowired
    private RedisTemplate<String, String> stringredis;//redis自带的注入就行

    @Resource
    SendssY sendssY;

    @GetMapping("Sendss")
    @Operation(summary = "输入手机号发送短信")
    public Boolean ha(String phone) {
        ValueOperations<String, String> opsForValue = stringredis.opsForValue();
        if (!stringredis.hasKey(phone)) {
            int i = (int) ((Math.random() * 9 + 1) * 100000);
            String code = String.valueOf(i);
            opsForValue.set(phone, code, 180, TimeUnit.SECONDS);
            sendssY.Send(phone, code);
            return true;
        } else {
            String code = opsForValue.get(phone);
            sendssY.Send2(phone, code);
            return false;
        }
    }
}
